import { getPage } from '../../utils/config';
import api from '../../utils/api';
import { http, extend, $toast, $loading } from '../../utils/utils';
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        getPage: getPage,
        tabsActive: 0,
        loading: 0,
        pageSize: 10,
        loadmore: false,
        userId: '',
    },

    /**
     * 切换显示
     */
    tabsChange(e){
        const { index } = e.detail;
        let { commentsInfo } = this.data;
        
        this.setData({ tabsActive: index });

        if (!commentsInfo[index].load) {
            this.setData({ loading: 0 });
            this.getCommentInfo((res) => {
                commentsInfo[index].list = res.list;
                this.setData({ commentsInfo, loading: 1 });
            });
        }
    },

    /**
     * 获取用户全部评论 -- 网路请求
     */
    getCommentInfo(cb, a = false) {
        const { tabsActive, userId, pageSize, commentsInfo  } = this.data;
        http.post(api.default, {
            data: {
                action: 'mycomments',
                page: commentsInfo[tabsActive].page,
                userid: userId,
                status: tabsActive ? 1 : 0,
            }
        }, a).then((res) => {
            const { data } = res.data;

            //判断是否已加载
            if (!commentsInfo[tabsActive].load) {
                commentsInfo[tabsActive].load = true;
            }

            //判断是否没有数据
            if (data.list.length == 0 && commentsInfo[tabsActive].page == 1) {
                commentsInfo[tabsActive].abnor = true;
            }

            //判断是否还有下一页
            if (data.list.length < pageSize) {
                commentsInfo[tabsActive].nextPages = false;
            }

            this.setData({ commentsInfo });

            cb && cb(data); 
        })
    },

    /**
     * 预览图片
     */
    picViewTap(e) {
        const { imageCdn } = api;
        const { tabsActive, commentsInfo } = this.data;
        let { src, parentIndex, mode } = e.currentTarget.dataset || e.target.dataset;
        let urls = [],
            picList = [],
            lists = commentsInfo[tabsActive].list[parentIndex];

        picList = Number(mode) ? lists.content_sec_pic : lists.pic_list;
        
        picList.forEach((item) => {
            urls.push(imageCdn + item);
        });

        wx.previewImage({
            current: imageCdn + src,
            urls: urls
        })
    },

    /**
     * 下拉刷新
     */
    handleToUpper() {
        let { tabsActive, commentsInfo } = this.data;
        
        $loading();

        //恢复默认请求参数
        commentsInfo[tabsActive] = extend({}, commentsInfo[tabsActive], {
            page: 1,
            abnor: false,
            load: false,
            nextPages: true,
        });

        this.setData({ commentsInfo });

        this.getCommentInfo((res) => {
            commentsInfo[tabsActive].list = res.list;
            this.setData({ commentsInfo });
            setTimeout(() => {
                this[`scroll${tabsActive}`].stop();
                wx.hideLoading();
            }, 1000)
        });
    },

    /**
     * 上拉加载
     */
    handleTolower() {
        let { tabsActive, loadmore, commentsInfo } = this.data;

        if (commentsInfo[tabsActive].nextPages && !loadmore) {
            commentsInfo[tabsActive].page++;
            this.setData({ commentsInfo, loadmore: true });

            this.getCommentInfo((res) => {
                commentsInfo[tabsActive].list = commentsInfo[tabsActive].list.concat(res.list);
                this.setData({ commentsInfo, loadmore: false });
            });
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        const { tabsActive }  = this.data;
        //是否登录
        app.getLogin().then((res) => {
            const { user } = res.data;
            this.setData({
                logged: res.code,
                userId: user.user_id,
                nickName: user.nickname,
            });
            
            //创建数据模型
            let commentsInfo = [];
            for (let i = 0; i <= 1; i++) {
                commentsInfo.push({
                    page: 1,
                    abnor: false,
                    load: false,
                    list: [],
                    nextPages: true,
                });
            }

            this.setData({ commentsInfo });

            this.getCommentInfo((res) => {
                commentsInfo[tabsActive].list = res.list;
                this.setData({ commentsInfo, loading: 1 });
            });

            for (let i = 0; i <= 1; i++) {
                this[`scroll${i}`] = this.selectComponent(`#scroll-${i}`);
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {
        
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    }
})